----------------------------------------------------------------------------------------------------------------------------------
--[[

    蜂箱额外收获组件

]]--
----------------------------------------------------------------------------------------------------------------------------------
---
    local function GetReplica(self)
        return self.inst.replica.hutao_com_bee_box_ex_harvest or self.inst.replica._.hutao_com_bee_box_ex_harvest
    end
    local function set_ex_result_str(self,str)
        local replica = GetReplica(self)
        if replica then
            replica:SetExResultStr(str)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_bee_box_ex_harvest = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}

    self._ex_result_str = "[]"
    self:AddOnSaveFn(function()
        self:Set("result_str", self._ex_result_str)
    end)
    self:AddOnLoadFn(function()
        self._ex_result_str = self:Get("result_str") or "[]"
    end)

end,
nil,
{
    _ex_result_str = set_ex_result_str,
})
------------------------------------------------------------------------------------------------------------------------------
--- 额外收获列表
    function hutao_com_bee_box_ex_harvest:AddExResult(prefab)
        local old_result = json.decode(self._ex_result_str)
        table.insert(old_result, prefab)
        self._ex_result_str = json.encode(old_result)
    end
    function hutao_com_bee_box_ex_harvest:GetExResult()
        return json.decode(self._ex_result_str)
    end
    function hutao_com_bee_box_ex_harvest:HasAnyExResult()
        local old_result = json.decode(self._ex_result_str)
        return #old_result > 0
    end
    function hutao_com_bee_box_ex_harvest:HasExResult(prefab)
        local num = 0
        for k, v in pairs(self:GetExResult()) do
            if v == prefab then
                num = num + 1
            end
        end
        return false, num
    end
    function hutao_com_bee_box_ex_harvest:ClearExResult()
        self._ex_result_str = "[]"
    end
------------------------------------------------------------------------------------------------------------------------------
-- 记录花的采集
    function hutao_com_bee_box_ex_harvest:PollinateFlower(prefab)
        local data = self:Get("pollinate_flower",{})
        data[prefab] = (data[prefab] or 0) + 1
        self:Set("pollinate_flower",data)
    end
    function hutao_com_bee_box_ex_harvest:GetAllPollinatedFlowers()
        return self:Get("pollinate_flower",{})
    end
    function hutao_com_bee_box_ex_harvest:ClearAllPollinatedFlowers()
        self:Set("pollinate_flower",{})
    end
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_bee_box_ex_harvest:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_bee_box_ex_harvest:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_bee_box_ex_harvest:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_bee_box_ex_harvest:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_bee_box_ex_harvest:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_bee_box_ex_harvest:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_bee_box_ex_harvest:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_bee_box_ex_harvest:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_bee_box_ex_harvest:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_bee_box_ex_harvest







